// controllers/permission_controller.go
package controllers

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"

	"go-admin-system/models"
)

type PermissionController struct {
	db *gorm.DB
}

func NewPermissionController(db *gorm.DB) *PermissionController {
	return &PermissionController{db}
}

// GetPermissions 获取所有权限
func (pc *PermissionController) GetPermissions(c *gin.Context) {
	var permissions []models.Permission
	if err := pc.db.Find(&permissions).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取权限列表失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"permissions": permissions})
}

// GetPermission 获取单个权限
func (pc *PermissionController) GetPermission(c *gin.Context) {
	var permission models.Permission
	if err := pc.db.First(&permission, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "权限不存在"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"permission": permission})
}

// CreatePermission 创建权限
func (pc *PermissionController) CreatePermission(c *gin.Context) {
	var input struct {
		Name        string `json:"name" binding:"required"`
		Description string `json:"description"`
		Status      int    `json:"status"`
	}

	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// 检查权限名是否已存在
	var existingPermission models.Permission
	if err := pc.db.Where("name = ?", input.Name).First(&existingPermission).Error; err == nil {
		c.JSON(http.StatusConflict, gin.H{"error": "权限名已存在"})
		return
	}

	permission := models.Permission{
		Name:        input.Name,
		Description: input.Description,
		Status:      input.Status,
	}

	if err := pc.db.Create(&permission).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "创建权限失败"})
		return
	}

	c.JSON(http.StatusCreated, gin.H{"permission": permission})
}

// UpdatePermission 更新权限
func (pc *PermissionController) UpdatePermission(c *gin.Context) {
	var permission models.Permission
	if err := pc.db.First(&permission, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "权限不存在"})
		return
	}

	var input struct {
		Name        string `json:"name"`
		Description string `json:"description"`
		Status      int    `json:"status"`
	}

	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// 检查新权限名是否已存在
	if input.Name != "" && input.Name != permission.Name {
		var existingPermission models.Permission
		if err := pc.db.Where("name = ?", input.Name).First(&existingPermission).Error; err == nil {
			c.JSON(http.StatusConflict, gin.H{"error": "权限名已存在"})
			return
		}
	}

	if input.Name != "" {
		permission.Name = input.Name
	}

	if input.Description != "" {
		permission.Description = input.Description
	}

	permission.Status = input.Status

	if err := pc.db.Save(&permission).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "更新权限失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"permission": permission})
}

// DeletePermission 删除权限
func (pc *PermissionController) DeletePermission(c *gin.Context) {
	var permission models.Permission
	if err := pc.db.First(&permission, c.Param("id")).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "权限不存在"})
		return
	}

	// 先删除关联表数据
	pc.db.Where("permission_id = ?", permission.ID).Delete(&models.RolePermission{})

	if err := pc.db.Delete(&permission).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "删除权限失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "权限删除成功"})
}
